Xbasic

SQL::ConnectionToJSON Method

Syntax

JSON as C = ToJSON(SQLStatement as C [, Arguments as SQL::Arguments] [, RowsToCopy = -1 as N [, StartRow = 1 as N [, ColumnReferences as SQL::TableInfo [, UserContext as P]]]])

Arguments

SQLStatementCharacter

A SQL SELECT statement.

ArgumentsSQL::Arguments

One or more arguments to be resolved when the SELECT statement is executed. Arguments is required if the SQL SELECT statement uses arguments. You can omit this parameter if no arguments are used by the SQL SELECT statement.

RowsToCopyNumeric

Default = -1 (all rows). The number of rows to copy.

StartRowNumeric

Default = 1 (first row). The first row to copy.

ColumnReferencesSQL::TableInfo

When data is formatted for a column in the result set: (1) if ReferenceColumns has a column with a matching name, that object will be used to format the data; (2) otherwise the ColumnInfo property of the result set is used to format the data.

UserContextPointer

The user context is passed into the evaluation of the expression when data is formatted as defined in the ColumnReferences.

Returns

JSONCharacter

Returns the result of the SQL Query as formatted JSON. Returns an empty string if no records are found.

Description

Fetch data to a JSON formatted string using a SQL query.

Discussion

The ToJSON() method converts the data in one or more rows to an implementation of JavaScript Object Notation format specific to the Alpha Anywhere AJAX implementation.

The function formats the data in column major order with indexes to map the rows using objects for each column with index, name, value and (optionally) HREF tags.

If the named expression is defined for a column, the function will generate the HREF tag using the expression provided. If ExternalFileNameExpression is set, the data will be stored to a file as well.

Example

This example retrieves the first 4 records from the orders table in the AADemo-Northwind database

dim cn as SQL::Connection

? cn.open("::Name::AADemo-Northwind")
= .T.

dim sql as c = "SELECT OrderID, OrderDate FROM orders"
dim rows as N = 4

? cn.ToJSON(sql,rows,1)
= [
{"OrderID" : "10248", "OrderDate" : "08/04/1994 12:00:00 000 am"},
{"OrderID" : "10249", "OrderDate" : "08/05/1994 12:00:00 000 am"},
{"OrderID" : "10250", "OrderDate" : "08/08/1994 12:00:00 000 am"},
{"OrderID" : "10251", "OrderDate" : "08/08/1994 12:00:00 000 am"}
]
You can add the AADemo-Northwind database connection string to your project through the AlphaDAO Connections builder using the Create AADemo-Northwind demo connection string link at the bottom of the dialog. The AlphaDAO Connections dialog can be accessed from the Edit menu on the Web Projects Control Panel. Select Edit > Manage Connection Strings > Alpha DAO Connection strings to open the builder.

This next example demonstrates using SQL::Arguments to fetch all orders with the CustomerID "CHOPS":

dim cn as SQL::Connection

? cn.open("::Name::AADemo-Northwind")
= .T.

dim sql as c = "SELECT OrderID, OrderDate FROM orders WHERE CustomerId = :CustomerId"
dim args as SQL::Arguments
args.set("CustomerID","CHOPS")
? cn.ToJSON(sql,args)
= [
{"OrderID" : "10254", "OrderDate" : "08/11/1994 12:00:00 000 am"},
{"OrderID" : "10370", "OrderDate" : "01/03/1995 12:00:00 000 am"},
{"OrderID" : "10519", "OrderDate" : "05/29/1995 12:00:00 000 am"},
{"OrderID" : "10731", "OrderDate" : "12/07/1995 12:00:00 000 am"},
{"OrderID" : "10746", "OrderDate" : "12/20/1995 12:00:00 000 am"},
{"OrderID" : "10966", "OrderDate" : "04/19/1996 12:00:00 000 am"},
{"OrderID" : "11029", "OrderDate" : "05/16/1996 12:00:00 000 am"},
{"OrderID" : "11041", "OrderDate" : "05/22/1996 12:00:00 000 am"}
]

See Also